import {createRouter, createWebHistory} from 'vue-router'
import Login from '../views/User/Login.vue'
import Register from '../views/User/Register.vue'
import Forget from '../views/User/Forget.vue'
import Find from '../views/User/Find.vue'
import {ElMessage} from "element-plus";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/home',// 首页
            name: 'Home',
            component: () => import('../views/Home/Home.vue'),
        },
        {
            path: '/detail/:id',// 详情页
            name: 'Detail',
            component: () => import('../views/Detail/Detail.vue')
        },
        {
            path: '/login',// 登录页
            name: 'Login',
            component: Login
        },
        {
            path: '/cart',// 购物车
            name: 'Cart',
            component: () => import('../views/Cart/Cart.vue')
        },
        {
            path: '/cart/pay',// 支付页面
            name: 'Pay',
            component: () => import('../views/Cart/Child/Pay.vue')
        },
        {
            path: '/order',// 订单页
            name: 'Order',
            component: () => import('../views/Order/Order.vue')
        },
        {
            path: '/center',// 个人中心
            name: 'Center',
            component: () => import('../views/Center/Center.vue'),
        },
        {
            path: '/center/address',// 地址管理
            name: "Address",
            component: () => import('../views/Center/Child/Address.vue')
        },
        {
            path: '/center/account',// 账号管理
            name: "Account",
            component: () => import('../views/Center/Child/Account.vue')
        },
        {
            path: '/center/information',// 信息管理
            name: "Information",
            component: () => import('../views/Center/Child/Information.vue')
        },
        {
            path: '/voucher',// 我的优惠券页
            name: 'Voucher',
            component: () => import('../views/Voucher/Voucher.vue')
        },
        {
            path: '/register',// 注册页
            name: 'Register',
            component: Register
        },
        {
            path: '/forget',// 忘记密码
            name: 'Forget',
            component: Forget
        },
        {
            path: '/find',// 密保重置
            name: 'Find',
            component: Find
        },
        {
            path: '/svg',
            name: 'SVG',
            component: () => import('../views/SVG/svg.vue')
        },
        {
            path: '/',
            redirect: '/svg'// 重定向到首页
        },
        {
            path: '/:pathMatch(.*)*',
            // 匹配不到的路由 跳转到 404页面
            name: 'NotFound',
            component: () => import('../views/404.vue')
        }
    ]
})

// 引入全局路由守卫
router.beforeEach((to, from, next) => {
// 除了登录页和注册页，其他页面都需要登录
    if (to.path !== '/login' && to.path !== '/register' && to.path !== '/forget' && to.path !== '/find') {
        if (localStorage.getItem('token')) {
            next()
        } else {
            ElMessage({
                message: '请先登录',
                type: 'warning'
            })
            next('/login')
        }
    } else {
        next()
    }
})
export default router
